import table from '@/components/dt/table/render.js'
import nameOpt from '@/biz/option/name.js'
import dateOpt from '@/biz/option/date.js'
import houseOpt from '@/biz/option/house.js'
import Opt from '@/components/dt/info/Opt.js'

let _this = null

class Column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      {
        title: '签约单编号',
        field: 'contractCode',
        minWidth: 150
      },
      {
        title: '任务状态',
        field: 'statusStr',
        minWidth: 150,
        slots: {
          default: (params, h) => {
            return params.row.statusStr ? params.row.statusStr : '-'
          }
        }
      },
      {
        title: '归属顾问',
        field: 'counselorName',
        minWidth: 130,
        header: {
          filter: nameOpt.create({
            key: 'counselorId',
            title: '归属顾问'
          })
        }
      },
      {
        field: 'customerName',
        minWidth: 150,
        title: '客户姓名',
        slots: {
          default: (params, h) => {
            return table.cell.btn(_this, h, {
              title: params.row.customerName,
              event: 'onLinkUser',
              data: params.row
            })
          }
        },
        header: {
          filter: {
            title: '客户姓名',
            key: 'customerName',
            placeholder: '请输入客户姓名',
            input: {}
          }
        }
      },
      {
        title: '客户手机号',
        field: 'customerPhone',
        minWidth: 150,
        header: {
          filter: {
            title: '客户手机号',
            key: 'customerPhone',
            placeholder: '请输入至少4位客户手机号',
            input: {},
            check: (val) => {
              if (val.customerPhone.length < 4) {
                _this.$Message.warning('请输入至少4位客户手机号。')
              }
              return !(val.customerPhone.length < 4)
            }
          }
        }
      },
      {
        title: '签约房源',
        field: 'roomName',
        minWidth: 120,
        header: {
          filter: houseOpt.createRoom({
            key: 'roomId',
            title: '签约房源'
          })
        }
      },
      {
        title: '面积(㎡)',
        field: 'area',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        title: '签约单价(元)',
        field: 'unitPrice',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        title: '签约总价(元)',
        field: 'totalPrice',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        title: '签约日期',
        field: 'contractDate',
        minWidth: 150,
        header: {
          filter: dateOpt.range({
            title: '签约日期',
            startKey: 'contractStartDate',
            endKey: 'contractEndDate',
            disabled: (date) => {
              return date.valueOf() > new Date().getTime()
            },
            check: (val) => {
              if (_this.$tool.dateDiff(val.contractStartDate, val.contractEndDate).Y >= 2) {
                // 日期最大跨度两年
                _this.$Message.warning('签约日期跨度至多两年。')
              }
              return !(_this.$tool.dateDiff(val.contractStartDate, val.contractEndDate).Y >= 2)
            }
          })
        }
      },
      {
        title: '计划回款日期',
        field: 'plannedPaymentDate',
        minWidth: 150,
        header: {
          filter: dateOpt.range({
            title: '计划回款日期',
            startKey: 'plannedPaymentStartDate',
            endKey: 'plannedPaymentEndDate',
            disabled: (date) => {
              return date.valueOf() > new Date().getTime()
            },
            check: (val) => {
              if (_this.$tool.dateDiff(val.plannedPaymentStartDate, val.plannedPaymentEndDate).Y >= 2) {
                // 日期最大跨度两年
                _this.$Message.warning('签约日期跨度至多两年。')
              }
              return !(_this.$tool.dateDiff(val.plannedPaymentStartDate, val.plannedPaymentEndDate).Y >= 2)
            },
            data: {
              from: (info) => {
                return info
                // if (info.productIds) {
                //   return info.productIds.split(',')
                // }
                // return null
              }
            }
          })
        }
      },
      {
        title: '回款类型',
        field: 'receiptTypeStr',
        minWidth: 160
      },
      {
        title: '计划回款金额(元)',
        field: 'plannedPaymentPrice',
        minWidth: 160,
        cell: {
          text: {
            type: 'number'
          }
        }
      },

      {
        title: '对应合同约定回款日期',
        field: 'agreementDate',
        minWidth: 180,
        header: {
          filter: dateOpt.range({
            title: '对应合同约定回款日期',
            startKey: 'agreementStartDate',
            endKey: 'agreementEndDate',
            disabled: (date) => {
              return date.valueOf() > new Date().getTime()
            },
            check: (val) => {
              if (_this.$tool.dateDiff(val.agreementStartDate, val.agreementEndDate).Y >= 2) {
                // 日期最大跨度两年
                _this.$Message.warning('签约日期跨度至多两年。')
              }
              return !(_this.$tool.dateDiff(val.agreementStartDate, val.agreementEndDate).Y >= 2)
            }
          })
        }
      },
      {
        title: '对应合同约定回款金额(元)',
        field: 'agreementMoney',
        minWidth: 200,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        title: '对应合同约定已回款金额(元)',
        field: 'creditMoney',
        minWidth: 220,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        title: '操作',
        fixed: 'right',
        width: 140,
        align: 'center',
        slots: {
          default: (params, h) => {
            let btns = []
            btns.push({
              title: _this.unlocking('0104030300') ? '查看' : '',
              event: 'viewFun',
              data: params.row
            })

            btns.push({
              title: _this.unlocking('0106020200') ? '代回款' : '',
              event: 'paymentTaskBtn',
              data: params.row
            })
            return table.cell.btns(_this, h, btns)
          }
        }
      }
    ]
  }
}

export default Column
